//
//  CollectMovieDB.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/11/4.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "CollectMovieDB.h"
#import <sqlite3.h>
@implementation CollectMovieDB
+(instancetype)shareDataBase{
    static CollectMovieDB *dataBase = nil;
    if(dataBase == nil){
        dataBase = [[CollectMovieDB alloc]init];
    }
    return dataBase;
}
static sqlite3 *db = nil;
//sqlite数据库是基于C语言的底层数据库

//打开数据库
-(void)openDB{
    if(db != nil){
        NSLog(@"collectionMovie数据库已经打开了");
        return ;
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"CollectMovie.sqlite"];
    NSLog(@"%@",sqlPath);
    //    需要转成utf8的中文编码
    BOOL result =  sqlite3_open(sqlPath.UTF8String, &db);
    if(result == SQLITE_OK){
        NSLog(@"yes 打开成功");
    }else{
        NSLog(@"no 打开失败");
    }
}
//建表
-(void)createTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS CollectMovie(number INTEGER PRIMARY KEY AUTOINCREMENT,movieId TEXT,movieName TEXT,username TEXT)";
    BOOL result =  sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"创建表成功=========");
    }
}
-(void)insertMovie:(MovieList *)movie userName:(NSString*)userName{
    
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO CollectMovie(movieId,movieName,username)VALUES ('%@','%@','%@')",movie.movieId,movie.movieName,userName];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"插入成功====");
    }else{
        NSLog(@"插入失败======	");
    }
}

-(NSMutableArray*)selectMoview:(NSString *)string{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM CollectMovie WHERE username = '%@'",string];
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"准备查询成功");
    }else{
        NSLog(@"查询失败");
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //        int number =  sqlite3_column_int(stmt, 0);
        const unsigned char *movieId = sqlite3_column_text(stmt, 1);
        //        取出第三列数据
        const unsigned char *movieName = sqlite3_column_text(stmt, 2);

        //        将C语言类型转换为OC的
        //        NSInteger num = number;
        NSString *movieid = [NSString stringWithUTF8String:(const char*)movieId];
        NSString* moviename = [NSString stringWithUTF8String:(const char*)movieName];
       

        MovieList *mov = [MovieList new];
        mov.movieId = movieid;
        mov.movieName = moviename;
        [array addObject:mov];
    }
    return array;
}
-(NSMutableArray*)selectAllMoview{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM CollectMovie"];
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"准备查询成功");
    }else{
        NSLog(@"查询失败");
    }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //        int number =  sqlite3_column_int(stmt, 0);
        const unsigned char *movieId = sqlite3_column_text(stmt, 1);
        //        取出第三列数据
        const unsigned char *movieName = sqlite3_column_text(stmt, 2);
        
        //        将C语言类型转换为OC的
        //        NSInteger num = number;
        NSString *movieid = [NSString stringWithUTF8String:(const char*)movieId];
        NSString* moviename = [NSString stringWithUTF8String:(const char*)movieName];
        
        MovieList *mov = [MovieList new];
        mov.movieId = movieid;
        mov.movieName = moviename;
        [array addObject:mov];
    }
    return array;
}

-(void)deleteMovie:(NSString*)name UserName:(NSString*)UserName{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM CollectMovie WHERE movieName = '%@' AND username = '%@'",name,UserName];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL,NULL , NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

-(void)dropTable{
    NSString *sql = [NSString stringWithFormat:@"DROP TABLE CollectMovie"];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"删除表成功");
    }else{
        NSLog(@"删除表失败");
    }
}


@end
